const app = getApp();

const RongIMLib = app.globalData.RongIMLib;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userData:[],            // 用户数据
        goodsDetail:[],         // 商品详情数据
        item: '../../img/img1.png',

		type:"",				// 详情类型  type : look 查看
        auction_id:'',          // 活动id
        goods_type:'',          // 商品类型
        sell_status:'',         // 是否售卖

        this_id:'',             // 自己id

        goods_type: '',          // 商品类型 ：1拍品 2团购 3 自由交易
    },

    /**
     * 生命周期函数--监听页面加载
     */
	// 收藏该商品
	collect(e){
        var self = this;
        var goodsDetail = this.data.goodsDetail;
        var goods_id = e.target.dataset.id || goodsDetail.id;
        var type = e.target.dataset.type || this.data.goodsDetail.focus_status;
		var userData = this.data.userData;

        if (type == 1) {
            var url = "/focus/goodscancel";
        }else{
            var url = "/focus/goods";
        }

		wx.request({
			url: app.url.requestUrl + url,
			method: 'POST',
			data: {
				openid: userData.openid,
				token: userData.token,
				id:goods_id
			},
			success: function (data) {
				var list = data.data
				if (list.code == 1) {
                    goodsDetail.focus_status = !goodsDetail.focus_status;
                    if (type == 1){
                        wx.showToast({
                            title: '取消收藏成功',
                        })
                    }else{
                        wx.showToast({
                            title: '收藏成功',
                        })
                    }
                    self.setData({goodsDetail})
				}else{
                    wx.showToast({
                        image:"../../img/warning.png",
                        title: list.msg,
                    })
                }
			}
		})
	},
	// 联系用户
	contact(e){
		var self = this;
		var url = "../my-chat/my-chat?user_id=goodsDetail.user_id";
		var this_id = this.data.this_id;
		var user_id = this.data.goodsDetail.user_id;
        var auction_id = this.data.auction_id;
        var name = this.data.goodsDetail.nick_name;
		if (this_id == user_id){
			wx.showToast({
				image:"../../img/warning.png",
				title: '该商家是自己'
			})
			return false;
		}

        if (auction_id){
            var chatRoomId = "chatRoomId" + auction_id;
            RongIMLib.RongIMClient.getInstance().quitChatRoom(chatRoomId, {
                onSuccess: function () {
                    console.log("退出聊天室成功" + chatRoomId);
                },
                onError: function (error) {
                    console.log("退出聊天室失败")
                }
            });
        }

		wx.navigateTo({
            url: `../my-chat/my-chat?user_id=${user_id}&name=${name}`,
		})
    },
    onLoad: function(options) {

		this.setData({
            goods_type: options.goods_type || '',
            sell_status: options.sell_status || '',
            auction_id: options.auction_id || '',
			type: options.type || "",
            this_id : app.globalData.User_id
		})

        console.log(options.sell_status)
        var self = this;
        if (options.id){
            wx.getStorage({
                key: 'user',
                success: function(res) {
                    self.setData({
                        userData:res.data
                    })
                    wx.request({
                        url: app.url.requestUrl + '/goods/goodsdetails',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                            id: options.id
                        },
                        success: function (data) {
                            var list = data.data;
                            if (list.code == 1) {
                                self.setData({
                                    goodsDetail: list.data,
                                })
                                wx.setNavigationBarTitle({ title: list.data.name })
                            }
                        }
                    })
                },
            })            
        }
    },
})